import os
import shutil

base_destination_root = "/mnt/usb/"
base_pics_root = "/app/ai/data/img"


def copy_images(source_file, destination_directory):
    # 确保目标目录存在
    os.makedirs(destination_directory, exist_ok=True)
    line_idx = 0
    # 逐行读取源文件
    with open(source_file, 'r') as file:
        for line in file:
            image_path = line.strip()  # 去除行末的换行符和空格
            if not image_path:
                continue  # 跳过空行

            line_idx += 1
            # 计算目标路径
            destination_path = f"{base_destination_root}{destination_directory}{image_path}"
            # 创建目标路径的文件夹
            os.makedirs(os.path.dirname(destination_path), exist_ok=True)

            source_image_path = f"{base_pics_root}{image_path}"

            # 复制文件
            shutil.copy(source_image_path, destination_path)
            print(f"{line_idx} :  {source_image_path} to {destination_path}")

# 使用示例
source_file = 'pics.txt'  # 包含图片相对路径的文件
destination_directory = 'imgs_1018'  # 目标文件夹

copy_images(source_file, destination_directory)
